package com.brillilab.service.core.space.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.inventory.ReagentInventoryMapper;
import com.brillilab.dao.mapper.space.PanelGridUsedMapper;
import com.brillilab.dao.mapper.space.PanelMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.enums.space.BoxTypeEnum;
import com.brillilab.domain.enums.space.SpaceTypeEnum;
import com.brillilab.domain.po.space.Panel;
import com.brillilab.domain.po.space.PanelGridUsed;
import com.brillilab.service.core.space.IPanelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: PanelServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/1419:06
 */
@Service
@Transactional
public class PanelServiceImpl implements IPanelService {
    @Autowired
    private PanelMapper panelMapper;
    @Autowired
    private PanelGridUsedMapper panelGridUsedMapper;
    @Resource
    private ReagentInventoryMapper reagentInventoryMapper;

    @Override
    public Long add(Panel panel) {
        if (!StringUtils.isEmpty(panel.getUserReagentType())) {
            panel.setUserReagentType("," + panel.getUserReagentType() + ",");
        }
        panelMapper.insert(panel);
        return panel.getId();
    }

    @Override
    public boolean addList(List<Panel> panelList) {
        panelList.forEach(p -> {
            if (!StringUtils.isEmpty(p.getUserReagentType())) {
                p.setUserReagentType("," + p.getUserReagentType() + ",");
            }
        });
        return panelMapper.insertList(panelList) > 0;
    }

    @Override
    public boolean delete(Long spaceId, SpaceTypeEnum spaceTypeEnum) {
        LambdaQueryWrapper<Panel> wrapper = new LambdaQueryWrapper<>();
        switch (spaceTypeEnum) {
            case ROOM:
                wrapper.eq(Panel::getRoomId, spaceId);
                break;
            case DEVICE:
                wrapper.eq(Panel::getDeviceId, spaceId);
                break;
        }
        Panel panel = new Panel();
        panel.setIsDelete(1);
        return panelMapper.update(panel, wrapper) > 0;
    }

    @Override
    public Panel get(Long panelId) {
        return panelMapper.selectById(panelId);
    }

    @Override
    public boolean update(Panel panel) {
        if (!StringUtils.isEmpty(panel.getUserReagentType())) {
            panel.setUserReagentType("," + panel.getUserReagentType() + ",");
        }
        return panelMapper.updateById(panel) > 0;
    }

    @Override
    public boolean updateInfo(Long panelId,String remark, String userReagentType, Long labMemberId) {
        if (!StringUtils.isEmpty(userReagentType)) {
            if (!userReagentType.startsWith(",")) {
                userReagentType = "," + userReagentType;
            }
            if (!userReagentType.endsWith(",")) {
                userReagentType = userReagentType+",";
            }
        }
        return panelMapper.updateInfo(panelId,remark, userReagentType, labMemberId) > 0;
    }

    @Override
    public boolean deleteList(List<Long> idList) {
        LambdaQueryWrapper<Panel> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Panel::getId, idList);
        Panel panel = new Panel();
        panel.setIsDelete(1);
        return panelMapper.update(panel, wrapper) > 0;
    }

    @Override
    public boolean deletePanelGridUsed(List<PanelGridUsed> list) {
        LambdaQueryWrapper<PanelGridUsed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PanelGridUsed::getPanelId, list.get(0).getPanelId());
        if (list.get(0).getBoxId() != null) {
            wrapper.eq(PanelGridUsed::getBoxId, list.get(0).getBoxId());
        } else {
            wrapper.eq(PanelGridUsed::getKitBoxId, list.get(0).getKitBoxId());
        }
        panelGridUsedMapper.delete(wrapper);
        return true;
    }

    @Override
    public List<PanelGridUsed> getPanelGridUsedList(Long panelId) {
        LambdaQueryWrapper<PanelGridUsed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PanelGridUsed::getPanelId, panelId);
        return panelGridUsedMapper.selectList(wrapper);
    }

    @Override
    public Integer addPanelGridUsedList(List<PanelGridUsed> list) {
        return panelGridUsedMapper.insertList(list);
    }

    @Override
    public Integer deletePanelGridUsedByBoxId(Long boxId, BoxTypeEnum boxTypeEnum) {
        LambdaQueryWrapper<PanelGridUsed> wrapper = new LambdaQueryWrapper<>();
        if (BoxTypeEnum.REAGENT_BOX.equals(boxTypeEnum)) {
            wrapper.eq(PanelGridUsed::getBoxId, boxId);
        } else {
            wrapper.eq(PanelGridUsed::getKitBoxId, boxId);
        }
        return panelGridUsedMapper.delete(wrapper);
    }

    @Override
    public Integer deletePanelGridUsedByBoxIdList(List<Long> boxIdList, BoxTypeEnum boxType) {
        LambdaQueryWrapper<PanelGridUsed> wrapper = new LambdaQueryWrapper<>();
        if (BoxTypeEnum.REAGENT_BOX.equals(boxType)) {
            wrapper.in(PanelGridUsed::getBoxId, boxIdList);
        } else {
            wrapper.in(PanelGridUsed::getKitBoxId, boxIdList);
        }
        return panelGridUsedMapper.delete(wrapper);
    }

    @Override
    public List<Panel> getPanelList(Long labId, Integer zone) {
        LambdaQueryWrapper<Panel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Panel::getLabId, labId);
        wrapper.eq(Panel::getZone, zone);
        wrapper.eq(Panel::getIsDelete, IsDeleteEnum.NO);
        return panelMapper.selectList(wrapper);
    }

    @Override
    public List<Panel> getPanelList(Long deviceId) {
        LambdaQueryWrapper<Panel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Panel::getDeviceId, deviceId);
        wrapper.eq(Panel::getIsDelete, 0);
        return panelMapper.selectList(wrapper);
    }

    @Override
    public boolean updateList(List<Panel> list) {
        list.forEach(p -> {
            if (!StringUtils.isEmpty(p.getUserReagentType())) {
                p.setUserReagentType("," + p.getUserReagentType() + ",");
            }
        });
        return panelMapper.updateList(list) > 0;
    }

    @Override
    public boolean deletePanelGridUsedByPanelIdList(List<Long> idList) {
        LambdaQueryWrapper<PanelGridUsed> query = new LambdaQueryWrapper<>();
        query.in(PanelGridUsed::getPanelId, idList);
        return panelGridUsedMapper.delete(query) > 0;
    }

    @Override
    public boolean deletePanelGridUsedByDeviceId(Long deviceId) {
        return panelGridUsedMapper.deleteByDeviceId(deviceId) > 0;
    }

    @Override
    public boolean deletePanelGridUsedByRoomId(Long roomId) {
        return panelGridUsedMapper.deleteByRoomId(roomId) > 0;
    }

    @Override
    public boolean updateRoomName(Long roomId, String name) {
        Panel panel = new Panel();
        panel.setRoomId(roomId);
        panel.setRoomName(name);
        LambdaQueryWrapper<Panel> query = new LambdaQueryWrapper<>();
        query.eq(Panel::getRoomId, roomId);
        panelMapper.update(panel, query);
        return true;
    }

    @Override
    public List<Panel> getListByIdList(List<Long> paneIdList) {
        LambdaQueryWrapper<Panel> query = new LambdaQueryWrapper<>();
        query.in(Panel::getId, paneIdList);
        return panelMapper.selectList(query);
    }

    @Override
    public List<PanelGridUsed> getPanelGridUsedListByIdList(List<Long> panelIdList) {
        LambdaQueryWrapper<PanelGridUsed> query = new LambdaQueryWrapper<>();
        query.in(PanelGridUsed::getPanelId, panelIdList);
        return panelGridUsedMapper.selectList(query);
    }

    @Override
    public List<PanelGridUsed> getPanelGridUsedListByBoxId(Long boxId, BoxTypeEnum boxType) {
        LambdaQueryWrapper<PanelGridUsed> wrapper = new LambdaQueryWrapper<>();
        if (BoxTypeEnum.REAGENT_BOX.equals(boxType)) {
            wrapper.eq(PanelGridUsed::getBoxId, boxId);
        } else {
            wrapper.eq(PanelGridUsed::getKitBoxId, boxId);
        }
        return panelGridUsedMapper.selectList(wrapper);
    }

    @Override
    public List<Panel> getLabPanelList(Long labId) {
        LambdaQueryWrapper<Panel> query = new LambdaQueryWrapper<>();
        query.eq(Panel::getLabId, labId);
        query.eq(Panel::getIsDelete, IsDeleteEnum.NO.getValue());
        return panelMapper.selectList(query);
    }

    @Override
    public List<PanelGridUsed> getPanelGridUsedListByBoxIdList(List<Long> boxIdList, BoxTypeEnum boxType) {
        LambdaQueryWrapper<PanelGridUsed> query = new LambdaQueryWrapper<>();
        if (BoxTypeEnum.REAGENT_BOX.equals(boxType)) {
            query.in(PanelGridUsed::getBoxId, boxIdList);
        } else {
            query.in(PanelGridUsed::getKitBoxId, boxIdList);
        }
        return panelGridUsedMapper.selectList(query);
    }

    @Override
    public boolean updateInventoryOwnerByPanelId(Long panelId, Long labMemberId) {
        return reagentInventoryMapper.updateOwnerByPanelId(panelId, labMemberId) > 0;
    }
}
